## World War 2 Appendix Dataset ##
## Prepared by Kevin Fahey ##
## And Doug Atkinson ##
## Prepared with R 4.2.1 ##

## Preamble ##
rm(list=ls())
set.seed(20120630)

library(foreign)
library(sandwich)
library(stargazer); library(xtable)
library(tidyverse)
library(haven)
library(plm)
library(pglm)
library(stringr)
library(maptools); library(usmap)
library(foreach)
library(zoo)
library(multiwayvcov); library(lmtest)
options(scipen=7)
options(digits=4)

# make sure you set your working directory here!!!

## Load in datasets ##
dat <- read.csv("dat.csv")
dat.analysis <- read.csv("datanalysis.csv")
final.statedat <- read.csv("finalstatedat.csv")
statedat <- read.csv("statedat.csv")



## Summary Tables ##
predict.out <- matrix(NA, nrow = 9, ncol = 6)
predict.trunc <- as.matrix(dat.analysis[,c("propelig", "swingcounty", "GOPAvg", 
                                           "pcturban", "totpop.thou", "avgcropvalue", 
                                           "pctmale.elig", "pctblack", "farmprop")])

for(i in 1:9){
  
  predict.out[i,1] <- colnames(predict.trunc)[i]
  predict.out[i,2] <- round(mean(na.omit(predict.trunc[,i])), 3)
  predict.out[i,3] <- round(sd(na.omit(predict.trunc)[,i]), 3)
  predict.out[i,4] <- round(min(na.omit(predict.trunc)[,i]), 3)
  predict.out[i,5] <- round(max(na.omit(predict.trunc)[,i]), 3)
  predict.out[i,6] <- length(na.omit(predict.trunc[,i]))
  
}

## and output ##
predict.out <- as.data.frame(predict.out)
rownames(predict.out) <- c("propelig", "swingcounty", "GOPAvg", 
                           "pcturban", "totpop.thou", "avgcropvalue", 
                           "pctmale.elig", "pctblack", "farmprop")
colnames(predict.out) <- c("Variable Name", "Mean", 
                           "Std. Dev", "Min", "Max", "Obs")

predict.print <- xtable(predict.out, digits = 3)
print(predict.print, include.rownames = F)


## Maps ##

# format map data 
datmap.swing <- dat[,c("fips", "swingcounty")]
dat42 <- dat[which(dat$year == 42 & dat$propelig <= 15), c("fips", "propelig")] # 3085
dat43 <- dat[which(dat$year == 43 & dat$propelig <= 15), c("fips", "propelig")] # 3085
dat44 <- dat[which(dat$year == 44 & dat$propelig <= 15), c("fips", "propelig")] # 3087
dat45 <- dat[which(dat$year == 45 & dat$propelig <= 15), c("fips", "propelig")] # 3087

# create z-scored measure of propelig by year
dat42$propelig <- (dat42$propelig - mean(na.omit(dat42$propelig)))/sd(na.omit(dat42$propelig))
dat43$propelig <- (dat43$propelig - mean(na.omit(dat43$propelig)))/sd(na.omit(dat43$propelig))
dat44$propelig <- (dat44$propelig - mean(na.omit(dat44$propelig)))/sd(na.omit(dat44$propelig))
dat45$propelig <- (dat45$propelig - mean(na.omit(dat45$propelig)))/sd(na.omit(dat45$propelig))

# Restrict obs to within 3 sd #
dat42 <- dat42[which(dat42$propelig >= -3 & dat42$propelig <= 3),]
dat43 <- dat43[which(dat43$propelig >= -3 & dat43$propelig <= 3),]
dat44 <- dat44[which(dat44$propelig >= -3 & dat44$propelig <= 3),]
dat45 <- dat45[which(dat45$propelig >= -3 & dat45$propelig <= 3),]


# swingstate map first
map.swing <- plot_usmap(regions = "counties",
                        data = datmap.swing,
                        values = "swingcounty",
                        labels = FALSE,
                        color = "gray", size = 0.1,
                        exclude = c("AK", "HI")) +
  scale_fill_continuous(low = "mediumorchid4", high = "goldenrod2") +
  labs(title = "Swing Counties, 1932-1940 U.S. Presidential Elections") +
  theme(legend.position = "none")
png("swingcountymap.png", res = 100)
print(map.swing)
dev.off()

# Enlistments 1942 next
map42 <- plot_usmap(regions = "counties", data = dat42,
                    values = "propelig", labels = FALSE,
                    color = "gray", size = 0,
                    exclude = c("AK", "HI")) +
  scale_fill_continuous(low = "purple4", high = "yellow1") +
  labs(title = "1942") +
  theme(legend.position = "none")
png("enlist42map.png", res = 120)
print(map42)
dev.off()

# Enlistments 1943 next
map43 <- plot_usmap(regions = "counties", data = dat43,
                    values = "propelig", labels = FALSE,
                    color = "gray", size = 0,
                    exclude = c("AK", "HI")) +
  scale_fill_continuous(low = "purple4", high = "yellow1",
                        name = "Z-scores",
                        label = scales::comma) +
  labs(title = "1943") +
  theme(legend.position = "none")
png("enlist43map.png", res = 120)
print(map43)
dev.off()

# Enlistments 1944 next
map44 <- plot_usmap(regions = "counties", data = dat44,
                    values = "propelig", labels = FALSE,
                    color = "gray", size = 0,
                    exclude = c("AK", "HI")) +
  scale_fill_continuous(low = "purple4", high = "yellow1") +
  labs(title = "1944") +
  theme(legend.position = "none")
png("enlist44map.png", res = 120)
print(map44)
dev.off()

# Enlistments 1945 next
map45 <- plot_usmap(regions = "counties", data = dat45,
                    values = "propelig", labels = FALSE,
                    color = "gray", size = 0,
                    exclude = c("AK", "HI")) +
  scale_fill_continuous(low = "purple4", high = "yellow1",
                        name = "",
                        label = scales::comma) +
  labs(title = "1945") +
  theme(legend.position = "right")
png("enlist45map.png", res = 120)
print(map45)
dev.off()